package com.winchain.chainbill.bill.biz.receive.rx;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbRcv;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillEvent;
import com.winchain.chainbill.bill.biz.receive.service.BillRcvTranService;
import com.winchain.chainbill.chain.ChainCallbackService;
import com.winchain.chainbill.trade.service.TradeService;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillRcvTranSubscriber extends OnErrorNotImplementedSubscriber<BillEvent<CbRcv>> {
	@Autowired
	private BillRcvTranService billRcvTranService;
	@Autowired
	private TradeService tradeService;
	@Autowired
	private ChainCallbackService chainCallbackService;

	public void onApplicationEvent(BillEvent<CbRcv> t) {
		CbRec rec = t.getBill().getRec();
		CbRcv cbRcv = (CbRcv) t.getBill().getSource();

		String bizType = "cb_rc_aprv";
		String txAbs = "������������";
		AcctTxRec tran = this.billRcvTranService.getAcctTxRec(rec, cbRcv, bizType, txAbs);

		Map<String, String> result = this.tradeService.fire(tran);
		try {
			String txSnHost = (String) result.get("tx_sn");
			t.setValue("txSnHost", txSnHost);
			this.chainCallbackService.callback(tran, txSnHost);
		} catch (Exception e) {
			e.printStackTrace();
			this.logger.error("BillRcvTran callback error!", e);
		}
	}
}
